<!--售票员票款差异待协查-->
<template>
  <div class="shouPiaoYuanPKCYDXC el-card is-always-shadow el-card__body">
    <avue-crud
      ref="crud"
      :option="option"
      :table-loading="listLoading"
      :data="data"
      :page.sync="page"
      @search-change="handleFilter"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="getList"
      :cell-style="cellStyle">
      <template slot="menuRight">
        <el-button type="primary"  size="mini" @click="replaceContent()">一键替换</el-button>
        <el-button type="primary"  size="mini" @click="sendStatus">下发协查</el-button>
        <el-button type="primary"  size="mini" @click="exportData">导出</el-button>
        <el-button type="primary"  size="mini" @click="delBatchs">删除</el-button>
      </template>
      <template slot-scope="scope" slot="lineCodeSearch">
        <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable filterable>
          <el-option
            v-for="(item,index) in lineCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="stationCodeSearch">
        <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" filterable clearable @change="changeStation">
          <el-option
            v-for="(item,index) in stationCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="conductorPersonNameSearch">
        <el-select v-model="searchForm.conductorPersonName" placeholder="请选择" style="width: 150px" filterable clearable>
          <el-option
            v-for="(item,index) in conductorPersonNames"
            :key="index"
            :label="item.nikename"
            :value="item.nikename">
          </el-option>
        </el-select>
      </template>
      <template slot="searchMenu" slot-scope="{row,size}">
        <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm);getList()}" icon="el-icon-delete">清空</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
<!--        <el-button type="text" v-if="scope.row.assistStatus!='3'" size="small" plain @click.stop="previewEditor(scope.row,scope.row.assistStatus)">预览</el-button>-->
        <el-button type="text" v-if="scope.row.assistState=='3'" size="small" plain @click.stop="previewEditor(scope.row)">回馈预览</el-button>
      </template>
    </avue-crud>
    <HuikuiInfo ref="HuikuiInfo" v-if="isShowDia"></HuikuiInfo>
</div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import moment from "moment";
    import {
      clearSelect,
      fenExYuangettoFixed,
      getgetStationCodes,
      getLineCodes, getLineName, getStationName,
      getXieChaStatusFormat, setSearchFiledsEmpty, getStationByCode,getLineAndStationByCode,
      xieChaStatus, getSysUserToLS,repalceStatus,getRepalceStatus,
    } from "../../../../../api/subwayRat/commonCheck";
    import HuikuiInfo from './HuikuiInfo';
    import {exportDataToExcel} from "../../../../../api/subwayRat/exportSelf";
    import {
      getSwAuConductorToBeCheckedList,
      replaceSwAuConductorToBeChecked, sendStatusSwAuConductorToBeChecked,delBatch
    } from "../../../../../api/subwayRat/conductorDifferences/conductorTicketChaYiShenhe/shouPiaoYuanCJDXY/shouPiaoYuanPKCYDXC/shouPiaoYuanPKCYDXC";
    import transitUtil from "../../../../../api/subwayRat/transitUtil";
    import {getPersonRole} from "../../../../../api/subwayRat/reportMondify/reportMistakeInfo";
    export default {
      components:{
        HuikuiInfo
      },
      data() {
            return {
              isShowDia:false,
              listLoading: true,
              lineCodes:[],
              stationCodes:[],
              conductorPersonNames:[],
              selectKeys:[], //存放对像
              selectKeyVals:[], //存放值
              data: [],
              searchForm:{
                lineCode:'',
                stationCode:''},
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条,
                isAsc: false// 是否倒序
              },
              option:{ },
            }
        },
        created() {
        this.getOption();
        },
        methods: {
          setSearchFiledsEmpty,
          refreshChange() {
            this.getList()
          },
          cellStyle({ row, column, rowIndex,columnIndex }) {
            if(13==columnIndex){
              if (row.diffMoney < 0 ) {
                return {
                  color: "red",
                };
              }else if (row.diffMoney > 0) {
                return {
                  color: "#bed742",
                };
              }
            }
          },
          getList() {
            this.listLoading = true;
            Object.assign(this.searchForm,this.page);
            let param={compareType:4};
            Object.assign(param,this.searchForm);
            getSwAuConductorToBeCheckedList(param).then((response)=>{
              this.data = response.data.data;
              this.listLoading = false;
              this.page.total=response.data.total;
            });
          },
          selectionChange(list){
            this.selectKeys=list;
            this.selectKeyVals=[];
            for(let i=0;i<list.length;i++){
              this.selectKeyVals.push(list[i].id);
            }
          },
          handleFilter(params, done) {
            Object.assign(this.searchForm,params);
            this.getList();
            done()
          },
          replaceContent(){
            if(this.selectKeys.length<=0){
              this.$message.warning("请选择替换数据！");
              return;
            }
            let ids=[];
            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].assistState!=3){
                this.$message.warning("请选择已协查状态数据数据！");
                return;
              }
            }
            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].replaceStatus==0){
                ids.push(this.selectKeys[i].id);
              }
            }
            if(ids.length<=0){
              this.$message.warning("无有效替换数据！");
              return;
            }
            replaceSwAuConductorToBeChecked({ids:ids.join(",")}).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.getList();
              }else{
                this.$message.error(res.data.msg);
              }
            })
          },
          previewEditor(row){ //回馈预览
            this.isShowDia=true;
            this.$nextTick(()=>{
              this.$refs.HuikuiInfo.recallShow(row);
            })
          },
          async sendStatus(){
            if(this.selectKeys.length<=0){
              this.$message.warning("请选择要设置的数据！");
              return;
            }
            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].assistState!=1){
                this.$message.warning("请选择未下发的数据！");
                return;
              }
            }

            let messageLSList = [];
            for(let i=0;i<this.selectKeys.length;i++){
              let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
              messageLSList.push({
                "content":"车站：" + data.data[0].name,
                "lineCode":this.selectKeys[i].lineCode,
                "stationCode":this.selectKeys[i].stationCode
              })
            }
            let p = {
              messageLSList:JSON.stringify(messageLSList),
              //title:this.$store.getters.tag.label,
	      title:'售票员票款差异待协查',
              msgType:"提交",
              //type:"shouPiaoYuanPKCYDXC_001",
              jdType:"shouPiaoYuanPKCYDXC_001",//机电
              kyType:"shouPiaoYuanPKCYDXC_002",//客运
              ids:this.selectKeyVals.join(",")
            };
            sendStatusSwAuConductorToBeChecked(p).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.getList();
              }else{
                this.$message.error("操作失败！");
              }
            }).catch(e=>{
              this.$message.error("操作失败！");
            })
          },
          changeLine(v){ //改变线路时
            getgetStationCodes(v).then(res=>{
              this.stationCodes=res;
              this.searchForm.stationCode='';
            })
          },
          changeStation(v){
            getSysUserToLS({lineCode:this.searchForm.lineCode, stationCode:v}).then((res)=>{
              this.conductorPersonNames=res.data.data;
            });
          },
          isassistInvestStatus(){ // 是否协查
            for(let i=0;i<this.selectKeys.length;i++){
              let row=this.selectKeys[i];
              if(row.assistStatus==3){
                return false
              }
            }
            return true;
          },
          async exportData(){ // 导出
            this.searchForm.pageNo=1;
            this.searchForm.pageSize=1000000000;
            if(this.selectKeys.length>0){
                let newData = JSON.parse(JSON.stringify(this.selectKeys));
                exportDataToExcel(newData,this.option.column,'ACC层级差异待协查');
            }else {
              let res=await getSwAuConductorToBeCheckedList(this.searchForm);
              let data = res.data.data;
              exportDataToExcel(data,this.option.column,'ACC层级差异待协查');
            }
          },
          delBatchs: function () {
            if (this.selectKeys.length<=0) {
              this.$message.error("请选择数据！");
              return false;
            }
            let ids = [];
            for(let i=0;i<this.selectKeys.length;i++){
              if (this.selectKeys[i].assistState != "1" || this.selectKeys[i].kyAssistInvestStatus != "1") {
                this.$message.error("请检查数据，只能删除待下发的数据！");
                return false;
              }
              ids.push(this.selectKeys[i].id);
            }
            this.$confirm("确认删除?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }).then(() => {
              delBatch(ids)
                .then((data) => {
                  this.$message.success("删除成功");
                  this.getList(this.page);
                  done();
                })
                .catch(() => {
                  loading();
                });
            });
          },
          getOption(){
            let th=this;
            this.option={
              emptyBtn:false,
              editBtn:false,
              delBtn:false,
              addBtn:false,
              border: true,
              index:true,
              indexLabel:'序号',
              page:true,
              align:'center',
              menuWidth:150,
              menuAlign:'center',
              selection: true,
              reserveSelection:false,
              height: 500,
              column:[ 
                {label:"线路",width: 100,
                "prop":"lineCode",type:'select',search:true,searchslot:true,formatter: function(row, value , label, column){
                  return getLineName(value);
                }},
                {label:"站点",width: 180,
                  "prop":"stationCode",type:'select',search:true,searchslot:true,formatter: function(row, value , label, column){
                  return getStationName(row.lineCode,value);
                }},
               
                {label:"售票员",width: 120,
                  "prop":"conductorPersonName",search:true,searchslot:true},
                {label:"收入日期",width: 120, "prop":"recordTime",type:'date',format:'yyyy-MM-dd',valueFormat:'yyyy-MM-dd',search:true},
                {label:"售票员操作号",width: 120,
                  "prop":"conductorPersonCode"},
                {label:"设备操作号",width: 120,
                  "prop":"equipmentNumber",type:'select'},
                {label:"BOM编号",width: 130,
                  "prop":"bomCode"},
                {label:"实收金额",
                  "prop":"paidAmount",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'paidAmount');
                  }},
                {label:"协查售票金额",width: 120,
                  "prop":"replySaleMoney",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'replySaleMoney');
                  }},
                {label:"协查充值金额",width: 120,
                  "prop":"replyRechargeMoney",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'replyRechargeMoney');
                  }},
                {label:"协查补票金额", width: 120,"prop":"replyErrorReturnMoney",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'replyErrorReturnMoney');
                  }},
                {label:"差异金额", "prop":"diffMoney",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'diffMoney');
                  }},
                {label:"加入协查时间",width: 120, "prop":"assistTime",type:'date',format:'yyyy-MM-dd',valueFormat:'yyyy-MM-dd'},
                {label:"机电协查状态",width: 120,
                  "prop":"assistState",type:'select',search:true,searchLabelWidth:100,dicData:xieChaStatus(),
                  formatter:function(row, value , label, column){
                    return getXieChaStatusFormat(value);
                  }},
                {label:"客运协查状态",width: 120,prop:"kyAssistInvestStatus",type:'select',search:true,searchLabelWidth:100,dicData:xieChaStatus(),formatter: function(row, value , label, column){
                    return getXieChaStatusFormat(value);
                  }},
                {prop:'replaceStatus',label:'替换状态',dicData:repalceStatus(),type:'select',search:true,
                  formatter:function(row, value , label, column){
                    return getRepalceStatus(value);
                  }},
                {prop:'sendPersonName',width:120,label:'下发人',}
              ]
            }
          },
          getChaYi(row,compareFirst,compareSec){ //结账金额 sc 差额
            if(!row[compareFirst] || !row[compareSec]){
              return '';
            }
            return Number(row[compareFirst])-Number(row[compareSec]);
          },
          otherVueAction(){ //供其他Vue页面调用
            let th=this;
            transitUtil.$on('shouPiaoYuanPKCYDXC_call',(param)=>{
              getSwAuConductorToBeCheckedList({differenceId:param.id}).then((response)=>{
                th.data = response.data.data;
                th.previewEditor(th.data[0]);
              });
            });
          }
        },
      mounted() {
        getLineCodes().then(res=>{ //获取线路
          this.lineCodes=res;
        });
        this.otherVueAction();
      }
    }
</script>
<style lang="scss">

</style>
